package com.springboot.demo.mapper;

import com.springboot.demo.entity.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ReaderBRMapper {
    //修改书籍状态为reserved
    @Update("update book set situation='reserved' where ID=#{bookid}" )
    void updateReserved(long bookid);

    //修改书籍状态为borrowed
    @Update("update book set situation='borrowed' where ID=#{bookid}" )
    void updateBorrowed(long bookid);

    //修改书籍状态为not borrowed
    @Update("update book set situation='not borrow' where ID=#{bookid}" )
    void updateNotBorrowed(long bookid);

    //修改书籍状态为borrowing
    @Update("update book set situation='borrowing' where ID=#{bookid}" )
    void updateBorrowing(long bookid);

    //修改reader表中某人的number，减1
    @Update("update reader set number=number-1 where(phone=#{phone} and rname = #{rname} and number >0)")
    void decReaderNum(String rname,String phone);

    //修改reader表中某人的number，加1
    @Update("update reader set number=number+1 where(phone=#{phone} and rname = #{rname} and number <3)")
    void incReaderNum(String rname,String phone);

    //根据bookid取Book
    @Select("select * from book where ID=#{bookid}")
    List<Book> getBook(long bookid);

    //根据rname与phone从reader表中取Reader
    @Select("select * from reader where rname=#{rname} and phone=#{phone}")
    List<Reader> getReaderByNP(String rname,String phone);

    //从reserve表中取全部Reserve
    @Select("select * from reserve")
    List<Reserve> getReserve();

    //根据rname与phone与bookid从reserve表中取Reserve
    @Select("select * from reserve where rname=#{rname} and phone=#{phone} and ID=#{bookid}")
    List<Reserve> getReserveByRPB(String rname,String phone,long bookid);

    //根据phone与rname与state='borrowing'从borrow表中取Borrow
    @Select("select * from borrow where phone=#{phone} and rname=#{rname} and state='borrowing'")
    List<BorrowMsg> getBorrowing(String phone,String rname);

    //根据phone与rname与bookid与state='borrowing'从borrow表中取Borrow
    @Select("select * from borrow where phone=#{phone} and ID=#{bookid} and rname=#{rname} and state='borrowing'")
    List<BorrowMsg> getBorrowingB(String phone,String rname,long bookid);

    //根据phone与rname与bookid与state='applying'从borrow表中取Borrow
    @Select("select * from borrow where phone=#{phone} and ID=#{bookid} and rname=#{rname} and state='applying'")
    List<BorrowMsg> getBorrowingA(String phone,String rname,long bookid);

    //根据phone与rname与bookid与state='borrowing'或'applying'从borrow表中取Borrow
    @Select("select * from borrow where phone=#{phone} and ID=#{bookid} and rname=#{rname} and (state='borrowing' or state='applying')")
    List<BorrowMsg> getBorrowingBA(String phone,String rname,long bookid);

    //根据phone与bookid与state='returning'从returntable表中取returning
    @Select("select * from returntable where phone=#{phone} and ID=#{bookid} and state='returning'")
    List<ReturnMsg> getReturningPB(String phone, long bookid);

    //向reserve表中添加某人reserve信息的phone与bookid与start_time
    @Insert("insert into reserve set phone=#{phone},ID=#{bookid},rname=#{rname},start_time=#{startTime}")
    void insertReserve(String phone,long bookid,String rname,String startTime);

    //向borrow表中插入borrow申请信息
    @Insert("insert into borrow set phone=#{phone},ID=#{bookid},start_time=#{startTime},state='applying',rname=#{rname}")
    void insertBorrow(String phone,String rname,long bookid,String startTime);

    //向return表中插入return申请信息
    @Insert("insert into returntable set phone=#{phone},ID=#{bookid},start_time=#{startTime},end_time=#{endTime},state='returning',overdue=#{overdue},days=#{days},fine=#{fine}")
    void insertReturn(String phone,long bookid,String startTime,String endTime,String overdue,long days,long fine);

    //根据phone与bookid删除reserve表中某人reserve信息
    @Delete("delete from reserve where phone=#{phone} and ID=#{bookid}")
    void deleteReserve(String phone,long bookid);

}
